﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using WpfApp.Services;
using WpfApp.Views;

namespace WpfApp.ViewModels
{
    public partial class ConsoleViewModel : ObservableObject
    {

        public enum TextType { debug, info, warning, error }

        public ConsoleView ConsoleView { get; set; }

        public ConsoleService ConsoleService { get; set; }
        public ConsoleViewModel()
        {


        }

        public ConsoleViewModel(ConsoleService consoleService)
        {
            ConsoleService = consoleService;
            ConsoleService.Clean = Clean;
            ConsoleService.Info = Info;
            ConsoleService.Warning = Warning;
            ConsoleService.Error = Error;
        }

        [RelayCommand]
        public void Clean()
        {
            ConsoleView.FlowDocument.Blocks.Clear();
        }
        
        public void Debug(string msg)
        {
            InsertMsg(msg, new SolidColorBrush(Colors.Black));

        }
        
        public void Info(string msg)
        {
            InsertMsg(msg, new SolidColorBrush(Colors.Green));

        }
        
        public void Warning(string msg)
        {
            InsertMsg(msg, new SolidColorBrush(Colors.YellowGreen));
        }

        

        public void Error(string msg)
        {
            InsertMsg(msg, new SolidColorBrush(Colors.Red));
        }

        /// <summary>
        /// 插入文本信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="color"></param>
        private void InsertMsg(string msg, SolidColorBrush color)
        {
            var text = new Run(msg);
            //添加样式
            text.FontWeight = FontWeights.Bold;
            text.Foreground = color;
            var insert = new Paragraph(text);
            //添加样式
            insert.Margin = new Thickness(0, 5, 0, 0);
            ConsoleView.FlowDocument.Blocks.Add(insert);
            ConsoleView.RichTextBox.ScrollToEnd();
        }
    }
}
